package nGui.controls;

import nGui.controls.Control;

class Text extends Control
{
	public var onChange(default,change) : Int -> String -> Int;
	public var cursor(getCursor,setCursor) : Int;
	public var maxchars(getMaxChars,setMaxChars) : Int;
	public var readonly(getReadOnly,setReadOnly) : Bool;
	public var border(null,setBorder) : Bool;

	private var __multiline : Bool;


	public function new( ?multiline : Bool )
	{
		super();
		__multiline = multiline;
		var val : String;
		val = "TEXT_CB";
		if ( multiline ) 
		{
			__c = ngui_multiline( untyped val.__s );
		} else {
			__c = ngui_text( untyped val.__s );
		}
	}

	private function change( func : Int->String->Int )
	{
		Reflect.setField(this,"onChange",func);
		this.setCallback( "TEXT_CB", onChange, null );
		return func;
	}

	public function append( val : String )
	{
		this.setAttribute( "APPEND", val );
		return null;
	}

	public function insert( val : String )
	{
		this.setAttribute( "INSERT", val );
		return null;
	}

	public function getCursor() : Int
	{
		return Std.parseInt( this.getAttribute( "CARET" ) );
	}

	public function setCursor( val : Int )
	{
		this.setAttribute( "CARET", val + "" );
		return null;
	}

	public function getMaxChars() : Int
	{
		return Std.parseInt( this.getAttribute( "NC" ) );
	}

	public function setMaxChars( val : Int )
	{
		this.setAttribute( "NC", val + "" );
		return null;
	}

	public function setReadOnly( val : Bool )
	{
		this.setAttribute( "READONLY", if ( val == true ) "YES" else "NO" );
		return null;
	}

	public function getReadOnly() : Bool
	{
		return ( this.getAttribute( "READONLY" ) == "YES" );
	}

	public function setBorder( val : Bool )
	{
		this.setAttribute( "BORDER", if ( val == true ) "YES" else "NO" );
		return null;
	}

	static var ngui_text = neko.Lib.load("nGui","ngui_element_text",1);
	static var ngui_multiline = neko.Lib.load("nGui","ngui_element_multiline",1);
}
